﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Model.SystemSetting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.DataMaintenance
{
    public class SFuJiaFeiYongWeiHu : IFuJiaFeiYongWeiHu
    {
        private readonly ISqlSugarClient db;
        public SFuJiaFeiYongWeiHu(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 查询附加费用和需要使用的数据信息
        /// </summary>
        /// <param name="strYuanQu"></param>
        /// <returns></returns>
        public string GetSelFuJiaFeiYongDataAll(string strYuanQu)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 附加费用数据

                List<jcsj_FuJiaFeiYong> listjcsj_FuJiaFeiYong = new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).getAll();

                #endregion

                #region 申请单数据

                List<QuanXianDataClass> listShenQingDanDataClass = new List<QuanXianDataClass>();
                List<jcsj_KeShi> listjcsj_KeShi = new SqlSugarRepository<jcsj_KeShi>(db).getByWhere(t => t.SuoShuYuanQu.Contains(strYuanQu)).OrderBy(t => t.ShunXu).ToList();
                foreach (var jcsj_KeShi in listjcsj_KeShi)
                {
                    QuanXianDataClass quanXianDataClass = new QuanXianDataClass();
                    quanXianDataClass.strCode = jcsj_KeShi.KeShiID.ToString();
                    quanXianDataClass.strName = jcsj_KeShi.KeShiMingCheng.ToString();
                    List<jcsj_ShenQingDan> listjcsj_ShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db).getByWhere(t => t.KeShiID == jcsj_KeShi.KeShiID && t.SuoShuYuanQu.Contains(strYuanQu))
                        .OrderBy(t => t.ShunXu).ToList();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var jcsj_ShenQingDan in listjcsj_ShenQingDan)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = "_" + jcsj_ShenQingDan.ShenQingDanID.ToString();
                        children.strName = jcsj_ShenQingDan.ShenQingDanMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    if (listchildren.Count != 0)
                    {
                        quanXianDataClass.children = listchildren;
                        listShenQingDanDataClass.Add(quanXianDataClass);
                    }
                }

                #endregion

                #region 费用类别

                List<string> listFeiYongLeiBie = new SqlSugarRepository<jcsj_ZiDian>(db)
                    .getByWhere(t => t.YiJiZiDianMingCheng == "附加费用相关" && t.ErJiZiDianMingCheng == "费用类别")
                    .Select(t => t.ZiDianZhiNeiRong).ToList();

                #endregion

                #region 费用性质

                List<string> listFeiYongXingZhi = new SqlSugarRepository<jcsj_ZiDian>(db)
                    .getByWhere(t => t.YiJiZiDianMingCheng == "附加费用相关" && t.ErJiZiDianMingCheng == "费用性质")
                    .Select(t => t.ZiDianZhiNeiRong).ToList();

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = new
                    {
                        listjcsj_FuJiaFeiYong,
                        listShenQingDanDataClass,
                        listFeiYongLeiBie,
                        listFeiYongXingZhi
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 附加费用添加
        /// </summary>
        /// <param name="fuJiaFeiYongData"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <returns></returns>
        public string PostFuJiaFeiYongAdd(FuJiaFeiYongDataClass fuJiaFeiYongData, string strYuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (!fuJiaFeiYongData.Validate("Add",out strFanHuiZhi)) goto PROC_EXIT;
                if (new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).getByWhere(t => t.FuJiaFeiYongMingCheng == fuJiaFeiYongData.strFuJiaFeiYongName).Any())
                {
                    nret = 1;
                    strFanHuiZhi = $@"附加费用名称【{fuJiaFeiYongData.strFuJiaFeiYongName}】已经存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 保存附加费用

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    jcsj_FuJiaFeiYong jcsj_FuJiaFeiYong = new jcsj_FuJiaFeiYong();
                    jcsj_FuJiaFeiYong.FuJiaFeiYongMingCheng = fuJiaFeiYongData.strFuJiaFeiYongName;
                    jcsj_FuJiaFeiYong.FuJiaFeiYongJiaGe = fuJiaFeiYongData.dFuJiaFeYongJiaGe.ToString();
                    jcsj_FuJiaFeiYong.FuJiaFeiYongXingZhi = fuJiaFeiYongData.strFuJiaFeiYongXinZhi;
                    jcsj_FuJiaFeiYong.ShenQingDanID = fuJiaFeiYongData.strShenQingDanId;
                    jcsj_FuJiaFeiYong.FeiYongLeiBie = fuJiaFeiYongData.strFeiYongLeiBie;
                    jcsj_FuJiaFeiYong.ShiFouTeShuHaoCai = fuJiaFeiYongData.strShiFouTeShuHaoCai;
                    jcsj_FuJiaFeiYong.ShiFouGuaGouJianYan = fuJiaFeiYongData.strShiFouGuaGouJianYan;
                    jcsj_FuJiaFeiYong.ShuLiang = fuJiaFeiYongData.iShuLiang;
                    jcsj_FuJiaFeiYong.BeiZhu1 = fuJiaFeiYongData.strBeiZhu1;
                    jcsj_FuJiaFeiYong.BeiZhu2 = fuJiaFeiYongData.strBeiZhu2;
                    int itid = new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).AddReturnIdentity(jcsj_FuJiaFeiYong);
                    jcsj_FuJiaFeiYong.FuJiaFeiYongID = strYuanQuQianZhui + itid;
                    if (new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).Update(t => new jcsj_FuJiaFeiYong
                    {
                        FuJiaFeiYongID = jcsj_FuJiaFeiYong.FuJiaFeiYongID
                    }, t => t.tid == itid) == false)
                    {
                        nret = 1000;
                        strFanHuiZhi = "添加失败！";
                        return;
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });


            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 附加费用修改
        /// </summary>
        /// <param name="fuJiaFeiYongData"></param>
        /// <returns></returns>
        public string PutFuJiaFeiYongUpdate(FuJiaFeiYongDataClass fuJiaFeiYongData)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 验证数据

                if (!fuJiaFeiYongData.Validate("Update", out strFanHuiZhi)) goto PROC_EXIT;
                if (new SqlSugarRepository<jcsj_FuJiaFeiYong>(db)
                    .getByWhere(t => t.FuJiaFeiYongMingCheng == fuJiaFeiYongData.strFuJiaFeiYongName && t.FuJiaFeiYongID == fuJiaFeiYongData.strFuJiaFeiYongId).Any())
                {
                    nret = 1;
                    strFanHuiZhi = $@"附加费用名称【{fuJiaFeiYongData.strFuJiaFeiYongName}】已经存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 修改数据

                if (!new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).Update(t => new jcsj_FuJiaFeiYong
                {
                    FuJiaFeiYongMingCheng = fuJiaFeiYongData.strFuJiaFeiYongName,
                    FuJiaFeiYongJiaGe = fuJiaFeiYongData.dFuJiaFeYongJiaGe.ToString(),
                    FuJiaFeiYongXingZhi = fuJiaFeiYongData.strFuJiaFeiYongXinZhi,
                    ShenQingDanID = fuJiaFeiYongData.strShenQingDanId,
                    FeiYongLeiBie = fuJiaFeiYongData.strFeiYongLeiBie,
                    ShiFouTeShuHaoCai = fuJiaFeiYongData.strShiFouTeShuHaoCai,
                    ShiFouGuaGouJianYan = fuJiaFeiYongData.strShiFouGuaGouJianYan,
                    ShuLiang = fuJiaFeiYongData.iShuLiang,
                    BeiZhu1 = fuJiaFeiYongData.strBeiZhu1,
                    BeiZhu2 = fuJiaFeiYongData.strBeiZhu2,
                }, t => t.FuJiaFeiYongID == fuJiaFeiYongData.strFuJiaFeiYongId))
                {
                    nret = 1000;
                    strFanHuiZhi = "修改失败！";
                    goto PROC_EXIT;
                }

                #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 附加费用删除
        /// </summary>
        /// <param name="fuJiaFeiYongDelete"></param>
        /// <returns></returns>
        public string DeleteFuJiaFeiYongDelete(FuJiaFeiYongDeleteClass fuJiaFeiYongDelete)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                if (!new SqlSugarRepository<jcsj_FuJiaFeiYong>(db).DeleteWhere(t => t.FuJiaFeiYongID == fuJiaFeiYongDelete.strFuJiaFeiYongId))
                {
                    nret = 1000;
                    strFanHuiZhi = "删除失败！";
                    goto PROC_EXIT;
                }


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
